package com.music.controller;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.Banner;
import com.music.service.BannerService;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerServiceImpl;

    /***
     * 新增
     * @param banner
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Banner banner) {
        int result = bannerServiceImpl.insert(banner);
        if (result == 0){
            return Result.failed("添加失败",result);
        }else {
            return Result.success("添加成功", result);
        }
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = bannerServiceImpl.delete(id);
        if (result == 0) {
            return Result.failed("删除失败", result);
        }else {
            return Result.success("删除成功", result);
        }
    }

    /***
     * 修改
     * @param banner
     * @param id
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public Result update(Integer id, Banner banner) {
        banner.setId(id);
        int result = bannerServiceImpl.update(banner);
        if (result == 0){
            return Result.failed("修改失败");
        }
        return Result.success("修改成功");
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        Banner banner = bannerServiceImpl.getByPrimaryKey(id);
        if (banner != null){
            return Result.success("查询成功", banner);
        }else {
            return Result.failed("查询失败");
        }
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Banner banner, int page, int size) {
        PageInfo<Banner> pageInfo = bannerServiceImpl.listPageInfo(banner, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Banner banner, int page, int size) {
        List<Banner> list = bannerServiceImpl.listPage(banner, page, size);
        return Result.success("查询成功", list);
    }
}